// pages/service/bill/index.js
const app=getApp()
Page({
  data: {
    filterData: '',
    items:[],
    active: 1,
    show: false,
    minDate: new Date(2019, 8, 1).getTime(),
    maxDate: new Date().getTime(),
    currentDate: '',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  onChange(event) {
    if (event.detail == "0") {
      wx.redirectTo({
        url: '../index',
      })
    } else if (event.detail == "1") {
      wx.redirectTo({
        url: './index',
      })
    } else if (event.detail == "2") {
      wx.redirectTo({
        url: './../servicer/servicer',
      })
      }
  },
// 确定选择日期弹窗
onConfirm(e) {
  if (!isNaN(e.detail)){
    var t = this.format(e.detail)
  }else{
    var t = this.format(new Date())
  }
  let local = wx.getStorageSync('local')
  app.api.getData('admin/order/time', {address:local,time:t}, (res) => {
    let data = res.data
    data.filter(item => {
      item.order_time = app.util.formatTime(item.order_time)
      var a = 0
      item.goods_list.forEach(e=>{
        a += e.id.sq_fencheng * e.value
      })
      item.cash_fee = a
    })
    this.setData({
      items: res.data,
      currentDate:t,
      show: false
    })
  })
},
// 关闭选择日期弹窗
onCancel() {
  this.setData({
    show: false
  })
},
showpopup(){
  this.setData({
    show:!this.data.show
  })
},
format(date) {
  const time = new Date(date)
  const year = time.getFullYear()
  const month = time.getMonth() + 1
  const day = time.getDate()

  return [year, month, day].map(this.formatNumber).join('-')
},
formatNumber(n){
  n = n.toString()
  return n[1] ? n : '0' + n
},
  received:function(e){
    var that = this;
    wx.showModal({
      title: '已收货 ',
      content: '确认客户收货了吗？',
      success: function (res) {
        if (res.confirm) {
          var index = e.currentTarget.dataset.haha;
          var data = e.currentTarget.dataset.id
          console.log(data)
          app.api.updateData('admin/orders', { data: data, a_status: '已收货' }, res => {
            console.log(res)
            if(res.data.ok==1){
              console.log(that.data.items[index])  
              var mtext = 'items[' + index + '].a_status'
              that.setData({
                [mtext]:'已收货'
              })
                            }
          })
          wx.showToast({
            title: '已收货',
            icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
            duration: 1000
          }) 
        } else {
          wx.showToast({
            title: '取消操作',
            icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
            duration: 1000
          }) 
        }
      }
    })

 
  },


  onLoad: function (options) {

    let t = this.format(new Date())
    let local = wx.getStorageSync('local')
    app.api.getData('admin/order/time', {address:local,time:t}, (res) => {
      let data = res.data
      data.filter(item => {
        item.order_time = app.util.formatTime(item.order_time)
        var a = 0
        item.goods_list.forEach(e=>{
          a += e.id.sq_fencheng * e.value
        })
        item.cash_fee = a
      })
      this.setData({
        items: res.data,
        currentDate:t
      })
    })
  }
     

  // onLoad:function(){
  
  // }
})